﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNhaThuoc.BL;
using QLNhaThuoc.DTO;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc
{
    public partial class Form_DMPhieuNhap : Form
    {
        public Form_DMPhieuNhap()
        {
            InitializeComponent();
        }

        PhieuNhapControl phieuNhapCtrl;
        BindingSource bs;
        Form_PhieuNhap frm;
        private void Form_DMPhieuNhap_Load(object sender, EventArgs e)
        {
            phieuNhapCtrl = new PhieuNhapControl();
            bs = new BindingSource();
            XuatListPhieuNhap();
            tìmToolStripMenuItem.Enabled = false;
        }

        private void XuatListPhieuNhap()
        {
            bs.DataSource = phieuNhapCtrl.LayListPhieuNhap();
            dgvPhieuNhap.DataSource = null;
            dgvPhieuNhap.AutoGenerateColumns = false;
            dgvPhieuNhap.DataSource = bs;
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            frm = new Form_PhieuNhap();
            frm.handle_formClosed += new Form_PhieuNhap.formClosed(frm_handle_formClosed);
            frm.MdiParent = this.MdiParent;
            frm.phieuNhapCtrl = phieuNhapCtrl;
            frm.isAdding = true;
            frm.Show();
        }

        void frm_handle_formClosed()
        {
            XuatListPhieuNhap();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thật sự muốn xóa không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;

            PhieuNhap info = (PhieuNhap)bs.Current;
            phieuNhapCtrl.XoaPN(info);
            XuatListPhieuNhap();
        }

        private void btXem_Click(object sender, EventArgs e)
        {
            if (bs.Count > 0)
            {
                frm = new Form_PhieuNhap();
                frm.handle_formClosed+=new Form_PhieuNhap.formClosed(frm_handle_formClosed);
                frm.MdiParent = this.MdiParent;
                frm.isViewing = true;
                frm.phieuNhapInfo = (PhieuNhap)bs.Current;
                frm.phieuNhapCtrl = phieuNhapCtrl;
                frm.Show();
            }
        }

        private void CheckMenuItem(object sender)
        {
            foreach (ToolStripMenuItem item in chọnTiêuChíTìmKiếmToolStripMenuItem.DropDownItems)
            {
                item.Checked = false;
            }
            ((ToolStripMenuItem)sender).Checked = true;
            tìmToolStripMenuItem.Enabled = true;
        }

        private void mãPhiếuNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void mãNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void ngàyNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void mãCôngTyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CheckMenuItem(sender);
        }

        private void tìmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tstbTim.Text != "")
            {
                SearchStrategy strategy = null;
                string value = tstbTim.Text;
                string expr = "";
                if (mãPhiếuNhậpToolStripMenuItem.Checked == true)
                {
                    strategy = new SearchStringStrategy();
                    strategy.SetParameter("MaPN", value);
                }
                else if (mãNhânViênToolStripMenuItem.Checked == true)
                {
                    strategy = new SearchStringStrategy();
                    strategy.SetParameter("MaNV", value);
                }
                else if (mãCôngTyToolStripMenuItem.Checked == true)
                {
                    strategy = new SearchStringStrategy();
                    strategy.SetParameter("MaCongTy", value);
                }
                else if (ngàyNhậpToolStripMenuItem.Checked == true)
                {
                    try
                    {
                        strategy = new SearchDateStrategy();
                        strategy.SetParameter("NgayNhap", value);
                    }
                    catch
                    {
                        MessageBox.Show("Bạn phải nhập ngày theo dạng dd/MM/yyyy");
                    }    
                }

                if(strategy != null)
                {
                    expr = strategy.GetSearchExpression();
                    bs.DataSource = phieuNhapCtrl.TimKiem(expr);
                    dgvPhieuNhap.DataSource = bs;
                }               
            }
            else
            {
                XuatListPhieuNhap();
            }
        }
    }
}
